import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 求笛卡尔积
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright:  Copyright (c) 2010</p>
 * <p>Company: DC </p>
 * 
 * @author lijwg
 * @date 2011-5-23
 * @version 1.0
 */
public class Descartes {
	Random r = new Random();

	/**
	 * 两个Map的笛卡尔积
	 * @param aMap
	 * @param bMap
	 * @return
	 */
	public Map<String, Integer> getDescartes(Map<String, Integer> aMap, Map<String, Integer> bMap) {
		Map<String, Integer> result = new HashMap<String, Integer>(aMap.size() * bMap.size());
		Iterator<String> ita = aMap.keySet().iterator();
		while(ita.hasNext()){
			String orderA = ita.next();
			Iterator<String> itb = bMap.keySet().iterator();
			while(itb.hasNext()){
				String orderB = itb.next();
				result.put(orderA+"*"+orderB, new Integer(aMap.get(orderA).intValue() * bMap.get(orderB).intValue()));
			}
		}
		return result;
	}
	
	public void setDefault(int[] array) {
		for (int i = 0; i < array.length; i++) {
			array[i] = i * r.nextInt();
		}
	}

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		Descartes descartes = new Descartes();
		
		int[] arrayA = new int[]{1,2,3};
		int[] arrayB = new int[100];
		int[] arrayC = new int[10000];
		descartes.setDefault(arrayA);
		descartes.setDefault(arrayB);
		descartes.setDefault(arrayC);
		List<int[]> arrayList = new ArrayList<int[]>();
		arrayList.add(arrayA);
		arrayList.add(arrayB);
		arrayList.add(arrayC);
		
		List mapList = new ArrayList();
		for (int[] arr : arrayList) {
			Map map = new HashMap();
			for (int i = 0; i < arr.length; i++) {
				map.put(String.valueOf(i), new Integer(arr[i]));
			}
			mapList.add(map);
		}
		
		long begin = System.currentTimeMillis();
		System.out.println("----begin:");
		Map resultMap = (Map) mapList.get(0);
		for(int i=1;i<mapList.size();i++){
			resultMap = descartes.getDescartes(resultMap,(Map)mapList.get(i));
		}
		System.out.println((System.currentTimeMillis() - begin)+ "ms");
		
		System.out.println(resultMap.size());
//		System.out.println(resultMap);
	}
}
